package com.zxx.libinject.apt;

import com.zxx.libinject.anno.ViewInjector;

import java.io.IOException;
import java.io.Writer;

import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.VariableElement;

/**
 * @author zxx on 2021/5/10
 */
public class DefaultJavaFileWriter extends AbsWriter {

    public DefaultJavaFileWriter(ProcessingEnvironment mProcessingEnv) {
        super(mProcessingEnv);
    }

    @Override
    protected void generateImport(Writer writer, InjectorInfo info) throws IOException {
        writer.write("package " + info.packageName + " ;");
        writer.write("\n\n");
        writer.write("import com.zxx.libinject.adapter.InjectAdapter;");
        writer.write("\n");
        writer.write("import com.zxx.injectandroid.utils.ViewFinder;");

        writer.write("\n\n\n");
        writer.write("/* This class is generated by Simple ViewInjector, please don't modify! */ ");
        writer.write("\n");

        writer.write("public class " + info.newClassName + " implements InjectAdapter<" + info.className + "> { ");
        writer.write("\n");
        writer.write("\n");
        // 查找方法
        writer.write("  public void injects(" + info.className + " target)  { ");
        writer.write("\n");
    }


    @Override
    protected void writeField(Writer writer, VariableElement element, InjectorInfo info) throws IOException {
        ViewInjector injector = element.getAnnotation(ViewInjector.class);
        String fieldName = element.getSimpleName().toString();

        writer.write("  target." + fieldName + " =  ViewFinder.findViewById(target, " + injector.value() + "  ) ; ");

        writer.write("\n");
    }

    @Override
    protected void writeEnd(Writer writer) throws IOException {
        writer.write("  }");
        writer.write("\n\n");
        writer.write(" } ");
    }
}
